﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;

namespace StellarMedical
{
    public static class ImageHelper
	{
	   public static void PreLoad(string imagePath)
	   {
	      const string JS = "var img = new Image(); img.src='{0}';";
	      HtmlPage.Window.Eval(string.Format(JS, AppUriHelper.GetRelativeUri(imagePath)));
	   }
	}

    public static class AppUriHelper
	{       
	   private static readonly Uri _baseUri;
	   private static readonly string _uriRoot;
	 
	   static AppUriHelper()
	   {
	      _uriRoot = System.Windows.Browser.HtmlPage.Document.DocumentUri.AbsoluteUri;
	      int lastSlash = _uriRoot.LastIndexOf("/");
	 
	      // we do this twice to get the "second to last" slash - leave this out if
	      // your app is hosted in the root, this assumes /subdir/mypage.aspx
	      _uriRoot = _uriRoot.Substring(0, lastSlash);
	      lastSlash = _uriRoot.LastIndexOf("/");
	      _uriRoot = _uriRoot.Substring(0, lastSlash + 1);
	 
	      _baseUri = new Uri(_uriRoot, UriKind.Absolute);
	   }
	 
	   public static Uri GetBaseUri()
	   {
	      return _baseUri;
	   }
	 
	   public static Uri GetRelativeUri(string relativePath)
	   {
	      string newUri = string.Format("{0}{1}", _uriRoot, relativePath);
	      return new Uri(newUri, UriKind.Absolute);
	   }
	}

    public partial class App : Application
    {

        public App()
        {
            this.Startup += this.Application_Startup;
            this.Exit += this.Application_Exit;
            this.UnhandledException += this.Application_UnhandledException;

            //ImageHelper.PreLoad("Image2.JPG");
            //ImageHelper.PreLoad("Image3.JPG");
            //ImageHelper.PreLoad("Image4.JPG");
            //ImageHelper.PreLoad("Image5.JPG");
            //ImageHelper.PreLoad("Image6.JPG");
            //ImageHelper.PreLoad("Image7.JPG");

            InitializeComponent();

            
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            this.RootVisual = new MainPage();
        }

        private void Application_Exit(object sender, EventArgs e)
        {

        }
        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            // If the app is running outside of the debugger then report the exception using
            // the browser's exception mechanism. On IE this will display it a yellow alert 
            // icon in the status bar and Firefox will display a script error.
            if (!System.Diagnostics.Debugger.IsAttached)
            {

                // NOTE: This will allow the application to continue running after an exception has been thrown
                // but not handled. 
                // For production applications this error handling should be replaced with something that will 
                // report the error to the website and stop the application.
                e.Handled = true;
                Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
            }
        }
        private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
        {
            try
            {
                string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
                errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");

                System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
            }
            catch (Exception)
            {
            }
        }
    }
}
